Swadhin Biswas Feature/ai code reviewer (#79) · 8/16/2026
cfd400d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import type { APIRoute } from "astro";
import { getDatabase, schema } from "@/db";
import { eq, and } from "drizzle-orm";
import { getUserFromRequest } from "@/lib/auth";
import { unauthorized, badRequest, success, notFound, forbidden, serverError } from "@/lib/api";
import { withErrorHandler } from "@/lib/errors";
import { logger } from "@/lib/logger";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { canAdminOrg } from "@/lib/permissions";
import { generateId } from "@/lib/utils";
import { z } from "zod";
/**
* Org-scoped SAML config CRUD (WS4-04).
* The samlConfigs table + validation machinery existed but nothing wrote
* config rows — this API closes the loop.
*/
const samlConfigSchema = z.object({
entityId: z.string().min(1, "IdP Entity ID required"),
ssoUrl: z.string().url("IdP SSO URL required"),
certificate: z.string().min(1, "IdP X.509 certificate required"),
signatureAlgorithm: z.string().optional().default("RSA-SHA256"),
digestAlgorithm: z.string().optional().default("SHA256"),
isEnabled: z.boolean().optional().default(true),
});
// GET: fetch the org's SAML config
export const GET: APIRoute = withErrorHandler(async ({ request, params }) => {
const user = await getUserFromRequest(request);
if (!user) return unauthorized();
const { org } = params;
const db = getDatabase() as NodePgDatabase<typeof schema>;
const organization = await db.query.organizations.findFirst({
where: eq(schema.organizations.name, org!),
});
if (!organization) return notFound("Organization not found");
if (!(await canAdminOrg(user.userId, organization.id))) return forbidden();
const config = await db.query.samlConfigs.findFirst({
where: eq(schema.samlConfigs.organizationId, organization.id),
});
return success({
saml: config
? {
id: config.id,
entityId: config.entityId,
ssoUrl: config.ssoUrl,
signatureAlgorithm: config.signatureAlgorithm,
digestAlgorithm: config.digestAlgorithm,
isEnabled: config.isEnabled,
// certificate is returned (admin-only endpoint); it is needed to
// configure the SP side and is sensitive — admin gate above
certificate: config.certificate,
}
: null,
});
});
// POST: create or update the org's SAML config
export const POST: APIRoute = withErrorHandler(async ({ request, params }) => {
const user = await getUserFromRequest(request);
if (!user) return unauthorized();
const { org } = params;
const body = await request.json();
const parsed = samlConfigSchema.safeParse(body);
if (!parsed.success) return badRequest("Invalid input", parsed.error);
const db = getDatabase() as NodePgDatabase<typeof schema>;
const organization = await db.query.organizations.findFirst({
where: eq(schema.organizations.name, org!),
});
if (!organization) return notFound("Organization not found");
if (!(await canAdminOrg(user.userId, organization.id))) return forbidden();
const existing = await db.query.samlConfigs.findFirst({
where: eq(schema.samlConfigs.organizationId, organization.id),
});
try {
if (existing) {
await db
.update(schema.samlConfigs)
.set({
entityId: parsed.data.entityId,
ssoUrl: parsed.data.ssoUrl,
certificate: parsed.data.certificate,
signatureAlgorithm: parsed.data.signatureAlgorithm,
digestAlgorithm: parsed.data.digestAlgorithm,
isEnabled: parsed.data.isEnabled,
updatedAt: new Date(),
})
.where(eq(schema.samlConfigs.id, existing.id));
logger.info({ userId: user.userId, orgId: organization.id }, "SAML config updated");
return success({ message: "SAML config updated", id: existing.id });
}
const id = generateId("saml");
await db.insert(schema.samlConfigs).values({
id,
organizationId: organization.id,
entityId: parsed.data.entityId,
ssoUrl: parsed.data.ssoUrl,
certificate: parsed.data.certificate,
signatureAlgorithm: parsed.data.signatureAlgorithm,
digestAlgorithm: parsed.data.digestAlgorithm,
isEnabled: parsed.data.isEnabled,
});
logger.info({ userId: user.userId, orgId: organization.id }, "SAML config created");
return success({ message: "SAML config created", id });
} catch (error) {
logger.error({ err: error }, "Failed to save SAML config");
return serverError("Failed to save SAML config");
}
});
// DELETE: remove the org's SAML config
export const DELETE: APIRoute = withErrorHandler(async ({ request, params }) => {
const user = await getUserFromRequest(request);
if (!user) return unauthorized();
const { org } = params;
const db = getDatabase() as NodePgDatabase<typeof schema>;
const organization = await db.query.organizations.findFirst({
where: eq(schema.organizations.name, org!),
});
if (!organization) return notFound("Organization not found");
if (!(await canAdminOrg(user.userId, organization.id))) return forbidden();
await db
.delete(schema.samlConfigs)
.where(eq(schema.samlConfigs.organizationId, organization.id));
return success({ message: "SAML config removed" });
});